<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-row>
              <el-col :span="24">
                <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-plus"
                  @click="addRoleBtn"
                >
                  新增角色</el-button>
              </el-col>
            </el-row>
            <!-- :data="tableData" -->
            <el-table
              :data="rolelist"
              style="width: 100%"
              border
            >
              <el-table-column
                prop="date"
                label="序号"
                width="120"
                type="index"
              />
              <el-table-column

                prop="name"
                label="角色名"
                width="180"
              />
              <el-table-column

                prop="description"
                label="描述"
              />
              <el-table-column
                prop="address"
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="setRoles(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoles(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-pagination
              :current-page="1"
              :page-sizes="[10, 15, 20, 30]"
              :page-size="100"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">

                <el-input v-model="companyInfo.companyAddress" style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" style="width:400px" />
              </el-form-item>
              <el-form-item label="备注" prop="remarks">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <!-- 绘制新增弹框 -->
        <el-dialog
          :title="isEdit ? '编辑角色' : '新增角色'"
          :visible.sync="showDialog"
          width="50%"
          @close="closeDialogFn"
        >
          <el-form
            ref="form"
            :model="ruleForm"
            :rules="rules"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="ruleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="ruleForm.description" />
            </el-form-item>
            <el-button size="small" @click="cancleRoles">取消</el-button>
            <el-button size="small" type="primary" @click="roleSubmit">确定</el-button>
          </el-form>
        </el-dialog>

        <!-- 角色分配弹框 -->
        <el-dialog
          title="分配权限"
          :visible.sync="dialogVisible"
          width="50%"
        >
          <assign-permission :children-id="childrenId" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRole, addRole, editRoleId, updateRoleAPI, delRoleAPI } from '@/api/department'
import { mapGetters } from 'vuex'
// 引入子组件
import AssignPermission from './assignPermission'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      dialogVisible: false,
      activeName: 'first',
      companyInfo: [], // 接收公司信息
      query: {
        page: 1, // 页码
        pagesize: 10 // 每页条数

      },
      ruleForm: {
        name: '',
        description: ''
      },
      // 添加角色验证
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      rolelist: [], // 员工角色列表
      total: 0, // 员工角色列表数量
      showDialog: false, // 弹框状态
      isEdit: false, // 弹框编辑状态
      childrenId: ''// 传给子组件的id
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    // 调用黑马企业信息方法
    this.getCompanyIofoFn()
    // 调用公司员工角色方法
    this.getRoleFn()
  },
  methods: {
    // 分配权限
    setRoles(id) {
      this.childrenId = id// 传给子组件的id
      this.dialogVisible = true// 弹出层展开
    },
    // 关闭弹框的时候+清空数据
    closeDialogFn() {
      this.ruleForm = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()// 清空检验
    },
    // 添加角色按钮
    addRoleBtn() {
      this.isEdit = false// 不是编辑状态
      this.showDialog = true// 让弹框展示
    },
    // 1.添加角色+兜底验证
    roleSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // 判断是编辑还是添加
        if (this.isEdit) {
          // 2.编辑角色
          const res = await updateRoleAPI(this.ruleForm)
          console.log(res)
          // this.ruleForm = res.data// 编辑信息在列表中展示
          this.isEdit = true// 是编辑
          this.showDialog = false// 弹窗关闭
          this.$message(res.message)// 给用户提示
        } else {
          // 1.添加角色
          const res = await addRole(this.ruleForm)
          // console.log(res)
          this.$message.success(res.message)// 给用户成功提示
          this.getRoleFn()// 刷新角色列表
          this.isEdit = false
          this.showDialog = false
          this.$message(res.message)// 给用户提示
        }
        this.getRoleFn()// 刷新列表
      })
    },

    // 取消按钮
    cancleRoles() {
      // 点击取消时需要清空表单数据,点+号时输入框为空

      this.showDialog = false
    },
    // 铺设员工角色列表
    async getRoleFn() {
      const res = await getRole(this.query)
      // console.log(res)
      this.rolelist = res.data.rows
      this.total = res.data.total
    },
    // 铺设黑马企业信息
    async getCompanyIofoFn() {
      const res = await getCompanyInfo(this.companyId)// 公司id
      // console.log(res)
      this.companyInfo = res.data
    },

    // 编辑角色
    async editRoles(id) {
      const res = await editRoleId(id)
      // console.log(res)
      this.ruleForm = res.data
      this.showDialog = true// 弹框展示
      this.isEdit = true
    },

    // 删除角色
    delRoles(id) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delRoleAPI(id)
        // console.log(res)
        // 如果当前员工角色页面剩一条数据,让页码自动往前走一页
        if (this.rolelist.length === 1) {
          this.query.page--
        }
        // 如果是第一页的最后一条数据,就留在第一页把
        if (this.rolelist.length <= 0) {
          this.query.page = 1
        }
        this.$message.success(res.message)
        this.getRoleFn()// 刷新角色列表
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 改变每条页数
    handleSizeChange(newSize) {
      this.query.pagesize = newSize
      this.getRoleFn()// 刷新角色列表
    },
    // 改变页码
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getRoleFn()// 刷新角色列表
    }

  }
}
</script>

<style lang="scss" scoped>
.el-table.el-table--fit.el-table--border.el-table--enable-row-hover {
     margin-top: 10px;

}
</style>
